package com.fastloan.mgt.daily.controller;

import com.fastloan.mgt.daily.dao.WorkLocationMapper;
import com.fastloan.mgt.daily.model.WCustomerManagerDaily;
import com.fastloan.mgt.daily.model.WorkLocation;
import com.fastloan.mgt.daily.model.vo.WCustomerManagerDailyVo;
import com.fastloan.mgt.daily.service.WCustomerManagerDailyService;
import com.fastloan.mgt.daily.service.WorkInfoService;
import com.fastloan.safe.core.util.ManagerKit;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author yanwe
 *         createTime 2017-08-2017/8/17 9:33
 */
@RestController
@RequestMapping("/api/chat/weiXin/managerDaily")
public class CustomerManagerDailyController extends BaseController<WCustomerManagerDaily> {

    @Autowired
    private WCustomerManagerDailyService customerManagerDailyService;

    @Autowired
    private WorkInfoService workInfoService;

    @PostMapping("")
    public ResultTo addWXDaily(WCustomerManagerDaily customerManagerDaily){
        return customerManagerDailyService.addDaily(customerManagerDaily);
    }

    @PutMapping("")
    public ResultTo updateWXDaily(WCustomerManagerDaily customerManagerDaily){
        return customerManagerDailyService.updateDaily(customerManagerDaily);
    }

    @DeleteMapping("/{dailyId}")
    public ResultTo deleteWXDaily(@PathVariable("dailyId") Integer dailyId){
        return customerManagerDailyService.deleteDaily(dailyId);
    }

    @GetMapping("")
    public ResultTo getWXDaily(@RequestParam(value = "dailyCommitId",required = false) Integer dailyCommitId){
        WCustomerManagerDailyVo daily;
        if(dailyCommitId == null){
            daily = customerManagerDailyService.getDaily(ManagerKit.getUserId(), LocalDateTime.now());
        } else {
            daily = customerManagerDailyService.getDaily(dailyCommitId);
        }
        return new ResultTo().setData(daily);
    }

    @PostMapping("/commit")
    public ResultTo commitDaily(Integer dailyCommitId,String remark){
        customerManagerDailyService.commitDaily(dailyCommitId,remark);
        return new ResultTo();
    }

    @GetMapping("/checkCommit")
    public ResultTo checkCommit(){
       return new ResultTo().setData(customerManagerDailyService.checkCommit(ManagerKit.getUserId()));
    }

    @GetMapping("/workInfo")
    public ResultTo getWorkInfo(){
        return workInfoService.getWorkInfo(ManagerKit.getTopOrgId());
    }

    @PostMapping("/workLocation")
    public ResultTo insertWorkLocation(WorkLocation workLocation){
        workLocation.setOrgId(ManagerKit.getTopOrgId());
        workLocation.setCreateTime(new Date());
        workLocation.setModifyTime(new Date());
        workLocation.setUserId(ManagerKit.getUserId());
        if (workLocation.getWorkLocationParentId() == null){
            workLocation.setWorkLocationParentId(0);
        }
        workInfoService.getWorkLocationMapper().insert(workLocation);
        return new ResultTo();
    }

    @PutMapping("/workLocation")
    public ResultTo updateWorkLocation(WorkLocation workLocation){
        WorkLocation location = workInfoService.getWorkLocationMapper()
                .selectByPrimaryKey(workLocation.getWorkLocationId());
        if (location == null){
            return new ResultTo(ResultEnum.OPERATION_FAILED,"该地点不存在！");
        }
        if (location.getUserId() == null){
            return new ResultTo(ResultEnum.OPERATION_FAILED,"不能删除默认地点！");
        }
        workInfoService.getWorkLocationMapper().updateByPrimaryKeySelective(workLocation);
        return new ResultTo();
    }

    @DeleteMapping("/workLocation/{workLocationId}")
    public ResultTo deleteWorkLocation(@PathVariable("workLocationId") Integer workLocationId){
        WorkLocationMapper workLocationMapper = workInfoService.getWorkLocationMapper();
        WorkLocation workLocation = workLocationMapper.selectByPrimaryKey(workLocationId);
        if (workLocation.getUserId() == null){
            return new ResultTo(ResultEnum.OPERATION_FAILED,"不能删除预置地点！");
        }
        int count = workLocationMapper.selectCount(new WorkLocation().setWorkLocationParentId(workLocationId));
        if (count != 0){
            return new ResultTo(ResultEnum.OPERATION_FAILED,"该地点有子地点,请先删除子地点！");
        }
        workLocationMapper.deleteByPrimaryKey(workLocationId);
        return new ResultTo();
    }
}
